package com.caigou.modules.forum.adpter

import android.app.Activity
import android.text.TextUtils
import android.util.TypedValue
import android.view.View
import android.widget.Button
import android.widget.ImageView
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.caigou.comm.R
import com.caigou.comm.base.bean.AppBackgroundInfo
import com.caigou.comm.base.bean.CommentItemBean
import com.caigou.comm.base.constants.AppConstants
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.ext.GlideDisplay
import com.caigou.comm.base.ext.levelImage
import com.caigou.comm.base.ext.showImgsPreview
import com.caigou.comm.base.ext.showToast
import com.caigou.comm.base.utils.ImageLoader
import com.caigou.comm.base.widget.SpaceItemDecoration
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.lwkandroid.widget.ngv.DefaultNgvAdapter
import com.lwkandroid.widget.ngv.NgvChildImageView
import com.lwkandroid.widget.ngv.NineGridView

/**
 * @author #Suyghur.
 * Created on 2021/11/20
 */
class CommentDetailItemAdapter(private val activity: Activity, dataset: MutableList<CommentItemBean>) :
    BaseQuickAdapter<CommentItemBean, BaseViewHolder>(R.layout.item_comment_detail_list, dataset) {

    init {
        addChildClickViewIds(R.id.item_comment_detail_iv_avatar)
        addChildClickViewIds(R.id.item_comment_detail_tv_report)
    }

    override fun convert(holder: BaseViewHolder, item: CommentItemBean) {
        // 最顶层评论
        if (TextUtils.isEmpty(item.replyUserInfoVo.headImg)) {
            holder.setImageResource(R.id.item_comment_detail_iv_avatar, R.drawable.img_comm_avatar)
        } else {
            ImageLoader.loadCircle(context, item.replyUserInfoVo.headImg, holder.getView(R.id.item_comment_detail_iv_avatar))
        }
        holder.setText(R.id.item_comment_detail_tv_nickname, item.replyUserInfoVo.nickName)
        holder.setText(R.id.item_comment_detail_tv_time, item.createTime)
        holder.setText(R.id.item_comment_detail_tv_content, item.content)
        holder.setImageResource(R.id.item_comment_detail_iv_lv, levelImage(item.replyUserInfoVo.level))

        holder.getView<NineGridView>(R.id.item_comment_detail_ngv_display).run {
            if (item.imgs.size > 0) {
                visibility = View.VISIBLE
                setSingleImageSize(TypedValue.COMPLEX_UNIT_DIP, 150, 200)
                val ngvAdapter = DefaultNgvAdapter(100, GlideDisplay())
                ngvAdapter.dataList = item.imgs
                ngvAdapter.setOnChildClickListener(object : DefaultNgvAdapter.OnChildClickedListener<String> {
                    override fun onPlusImageClicked(plusImageView: ImageView?, dValueToLimited: Int) {

                    }

                    override fun onContentImageClicked(position: Int, data: String?, childImageView: NgvChildImageView?) {
                        data?.apply {
                            showImgsPreview(activity, item.imgs, item.imgs.indexOf(this))
                        }
                    }

                    override fun onImageDeleted(position: Int, data: String?) {
                    }

                })
                adapter = ngvAdapter
            } else {
                visibility = View.GONE
            }
        }

        if (item.replyChildren.size <= 0) {
            holder.getView<RecyclerView>(R.id.item_comment_detail_recycler_view).visibility = View.GONE
        } else {
            holder.getView<RecyclerView>(R.id.item_comment_detail_recycler_view).run {
                visibility = View.VISIBLE
                layoutManager = LinearLayoutManager(activity)
                val itemAdapter = CommentDetailReplyItemAdapter(activity, item, mutableListOf())
                adapter = itemAdapter
                itemAdapter.setList(item.replyChildren)
                itemAnimator = DefaultItemAnimator()
                addItemDecoration(SpaceItemDecoration(activity))
            }
        }

        holder.getView<Button>(R.id.item_comment_detail_btn_reply).setOnClickListener {
            if (AppBackgroundInfo.instance.hasLogin) {
                ARouter.getInstance()
                    .build(RouterPath.Forum.PAGE_COMMENT_REPLY)
                    .withObject(AppConstants.COMMENT_ITEM_BEAN, item)
                    .withObject(AppConstants.COMMENT_ROOT_ITEM_BEAN, item)
                    .navigation()
            } else {
                context.showToast("请您先登录")
            }
        }
    }
}